package com.teste;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class TestProvider {
	private ArrayList<Pair<String, String> > listFiles;

	public ArrayList<Pair<String, String> > getFileTests(String basePath){
		listFiles = new ArrayList<Pair<String, String>>();

		File baseDir = new File(basePath);
		this.readDirectory(baseDir);
		
		return listFiles;
	
	}

	private void readDirectory(File file){
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++)
				readDirectory(files[i]);
		}else{
			readFile(file);
		}

	}

	private void  readFile(File file) {
		String fileName = file.getName();
		String fileContent = getFileContent(file);

		Pair<String, String> newFile = new Pair<String, String>(fileName, fileContent);

		listFiles.add(newFile);

	}

	private String getFileContent(File file){
		String fileContent = "";
		
		try{
			FileInputStream fstream = new FileInputStream(file);

			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;

			while ((strLine = br.readLine()) != null)   {
				fileContent += " " + strLine;
			}

			in.close();
		}catch (Exception e){
			System.err.println("Error: " + e.getMessage());
		}
	
		return fileContent;
	}
}
